package com.hexb.smh.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.hexb.core.utils.RegexUtils;
import com.hexb.smh.entity.enums.RoleType;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.Pattern;
import java.util.Date;

/**
 * @author : hexb
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Account {

    //id
    @ApiModelProperty("id")
    private Integer id;

    @Pattern(regexp = "[a-zA-Z\\d@.]{4,20}", message = "用户名只能4-20位[数字/字母/@.]")
    @ApiModelProperty("登录名")
    private String loginName;

    @Pattern(regexp = "\\u4e00-\\u9fa5")
    @ApiModelProperty("姓名")
    private String name;

    @Pattern(regexp = RegexUtils.MOBILE_CN)
    @ApiModelProperty("国内手机号")
    private String phoneNo;

    @JsonIgnore
    @ApiModelProperty("密码")
    private String password;

    @ApiModelProperty("是否启用(禁用帐号不能登录)")
    private Boolean enabled;

    @ApiModelProperty("是否激活(未激活帐号不能使用功能,登录时要求修改密码)")
    private Boolean activated;

    @ApiModelProperty("角色")
    private RoleType role;

    @ApiModelProperty("头像(目前为随机头像)")
    private String avatar;

    @ApiModelProperty("备注")
    private String remarks;

    @ApiModelProperty("创建时间")
    @JsonFormat(
            pattern = "yyyy-MM-dd HH:mm:ss",
            timezone = "GMT+8"
    )
    private Date createTime;


    @ApiModelProperty("最后一次修改时间")
    @JsonFormat(
            pattern = "yyyy-MM-dd HH:mm:ss",
            timezone = "GMT+8"
    )
    private Date updateTime;

    @ApiModelProperty("创建人")
    private Integer createBy;

    @ApiModelProperty("最后一次修改人")
    private Integer updateBy;

    @ApiModelProperty("最后一次登录时间")
    @JsonFormat(
            pattern = "yyyy-MM-dd HH:mm:ss",
            timezone = "GMT+8"
    )
    private Date lastLoginTime;

    private String lastLoginIp;

    //是否是一个管理员
    public boolean isAdmin() {
        return role == RoleType.admin || role == RoleType.super_admin || role == RoleType.salesman_manager;
    }

    //是否是一个业务员
    public boolean isSalesman() {
        return role == RoleType.salesman;
    }
}
